/*
 * ModificarMedicamentos.java
 *
 * Created on 13-dic-2011, 13:02:28
 */

package interfazgrafica.modificar.medicamentos;

import interfazgrafica.ImprimirInformes;
import interfazgrafica.ayuda.VentanaAyuda;
import interfazgrafica.modificar.alimentos.AñadirAlimentos;
import interfazgrafica.modificar.alimentos.EliminarAlimentos;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lauaa
 */
public class ModificarMedicamentos extends javax.swing.JFrame {
    private final Connection conn;
    private final Statement stmt;
    private String[] args;

    /** Creates new form ModificarMedicamentos */
    public ModificarMedicamentos() throws ClassNotFoundException, SQLException {
        initComponents();
        //Accedo a la base de datos.
            Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "system", "terrano89");
            stmt = conn.createStatement();

        //Hago una consulta y la guardo en rs.
	ResultSet rs = null;
            rs = stmt.executeQuery("SELECT fechacompra, medicamento, id, receta FROM Medicamentos");

                DefaultTableModel modelo = new DefaultTableModel();
                jTable1.setModel(modelo);

                // Creamos las columnas.
                modelo.addColumn("Fecha de Compra");
                modelo.addColumn("Medicamento");
                modelo.addColumn("Id");
                modelo.addColumn("Receta");

                // Bucle para cada resultado en la consulta
                while (rs.next())
                {
                   // Se crea un array que será una de las filas de la tabla.
                   Object [] fila = new Object[4]; // Hay tres columnas en la tabla

                   // Se rellena cada posición del array con una de las columnas de la tabla en base de datos.
                   for (int i=0;i<4;i++)
                      fila[i] = rs.getObject(i+1); // El primer indice en rs es el 1, no el cero, por eso se suma 1.

                   // Se añade al modelo la fila completa.
                   modelo.addRow(fila);
                }
                jTable1.setModel(modelo);

            rs.close();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextFieldId = new javax.swing.JTextField();
        jTextFieldMedicamento = new javax.swing.JTextField();
        jTextFieldReceta = new javax.swing.JTextField();
        jTextFieldFecha = new javax.swing.JTextField();
        jLabelReceta = new javax.swing.JLabel();
        jLabelFecha = new javax.swing.JLabel();
        jLabeMedicamento = new javax.swing.JLabel();
        jLabelId = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuModificar = new javax.swing.JMenu();
        jMenuItemAñadir = new javax.swing.JMenuItem();
        jMenuItemEliminar = new javax.swing.JMenuItem();
        jMenuItemImprimir = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jTextFieldId.setName("jTextFieldId"); // NOI18N

        jTextFieldMedicamento.setName("jTextFieldMedicamento"); // NOI18N

        jTextFieldReceta.setName("jTextFieldReceta"); // NOI18N

        jTextFieldFecha.setName("jTextFieldFecha"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cowmanager.CowManagerApp.class).getContext().getResourceMap(ModificarMedicamentos.class);
        jLabelReceta.setText(resourceMap.getString("jLabelReceta.text")); // NOI18N
        jLabelReceta.setName("jLabelReceta"); // NOI18N

        jLabelFecha.setText(resourceMap.getString("jLabelFecha.text")); // NOI18N
        jLabelFecha.setName("jLabelFecha"); // NOI18N

        jLabeMedicamento.setText(resourceMap.getString("jLabeMedicamento.text")); // NOI18N
        jLabeMedicamento.setName("jLabeMedicamento"); // NOI18N

        jLabelId.setText(resourceMap.getString("jLabelId.text")); // NOI18N
        jLabelId.setName("jLabelId"); // NOI18N

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenuModificar.setText(resourceMap.getString("jMenuModificar.text")); // NOI18N
        jMenuModificar.setName("jMenuModificar"); // NOI18N

        jMenuItemAñadir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemAñadir.setText(resourceMap.getString("jMenuItemAñadir.text")); // NOI18N
        jMenuItemAñadir.setName("jMenuItemAñadir"); // NOI18N
        jMenuItemAñadir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAñadirActionPerformed(evt);
            }
        });
        jMenuModificar.add(jMenuItemAñadir);

        jMenuItemEliminar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemEliminar.setText(resourceMap.getString("jMenuItemEliminar.text")); // NOI18N
        jMenuItemEliminar.setName("jMenuItemEliminar"); // NOI18N
        jMenuItemEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemEliminarActionPerformed(evt);
            }
        });
        jMenuModificar.add(jMenuItemEliminar);

        jMenuItemImprimir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemImprimir.setText(resourceMap.getString("jMenuItemImprimir.text")); // NOI18N
        jMenuItemImprimir.setName("jMenuItemImprimir"); // NOI18N
        jMenuItemImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemImprimirActionPerformed(evt);
            }
        });
        jMenuModificar.add(jMenuItemImprimir);

        jMenuBar1.add(jMenuModificar);

        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelFecha)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelId)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                        .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabeMedicamento)
                    .addComponent(jLabelReceta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextFieldMedicamento, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addComponent(jTextFieldReceta, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                .addGap(55, 55, 55))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(288, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabeMedicamento)
                    .addComponent(jTextFieldMedicamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelFecha)
                    .addComponent(jTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelReceta)
                    .addComponent(jTextFieldReceta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelId)
                    .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(84, 84, 84))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(173, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        try {
            //Hago una consulta y la guardo en rs2.
            ResultSet rs2 = null;
            String consulta = "SELECT * FROM Medicamentos WHERE receta = '";
            consulta = consulta + jTable1.getValueAt(jTable1.getSelectedRow(), 3) + "'";
            rs2 = stmt.executeQuery(consulta);
            while (rs2.next()) {
                jTextFieldReceta.setText(rs2.getString("receta"));
                jTextFieldId.setText(rs2.getString("id"));
                jTextFieldMedicamento.setText(rs2.getString("medicamento"));
                jTextFieldFecha.setText(rs2.getString("fechacompra"));
            }
            rs2.close();
        } catch (SQLException ex) {
            Logger.getLogger(ModificarMedicamentos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jMenuItemAñadirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAñadirActionPerformed
        AñadirMedicamentos.main(args);
        this.dispose();
}//GEN-LAST:event_jMenuItemAñadirActionPerformed

    private void jMenuItemEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemEliminarActionPerformed
        EliminarMedicamentos.main(args);
        this.dispose();
}//GEN-LAST:event_jMenuItemEliminarActionPerformed

    private void jMenuItemImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemImprimirActionPerformed
        ImprimirInformes.main(args);
}//GEN-LAST:event_jMenuItemImprimirActionPerformed

    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
        VentanaAyuda ayuda = new VentanaAyuda("Modificar medicamentos");
        ayuda.setVisible(true);
    }//GEN-LAST:event_jMenu2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        VentanaAyuda ayuda = new VentanaAyuda("Modificar medicamentos");
        ayuda.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ModificarMedicamentos().setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ModificarMedicamentos.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(ModificarMedicamentos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabeMedicamento;
    private javax.swing.JLabel jLabelFecha;
    private javax.swing.JLabel jLabelId;
    private javax.swing.JLabel jLabelReceta;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItemAñadir;
    private javax.swing.JMenuItem jMenuItemEliminar;
    private javax.swing.JMenuItem jMenuItemImprimir;
    private javax.swing.JMenu jMenuModificar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldFecha;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldMedicamento;
    private javax.swing.JTextField jTextFieldReceta;
    // End of variables declaration//GEN-END:variables

}
